package dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import utils.DbUtil;
import entity.ChallengeInfo;
import entity.User;

public class Jdbcuserdao implements Userdao {
	private PreparedStatement stmt;
	private ResultSet rs;

	public void save(User user) throws Exception {
		String sqlstr = "insert into VS_USER (name,email,tel,play,money,area,address,password) values (?,?,?,?,?,?,?,?)";

		Connection conn = DbUtil.getConnection();

		PreparedStatement pst = conn.prepareStatement(sqlstr);
		pst.setString(1, user.getName());
		pst.setString(2, user.getEmail());
		pst.setString(3, user.getTel());
		pst.setString(4, user.getPlay());
		pst.setDouble(5, user.getAccount());
		pst.setString(6, user.getArea());
		pst.setString(7, user.getAddress());
		pst.setString(8, user.getPassword());
		pst.executeUpdate();
		DbUtil.close(conn);
	}

	public boolean check(String mail) throws Exception {

		String s1 = "select * from VS_USER where email=?";
		Connection conn = DbUtil.getConnection();
		System.out.println(mail);
		stmt = conn.prepareStatement(s1);
		stmt.setString(1, mail);
		System.out.println("mail344");
		rs = stmt.executeQuery();
		System.out.println("mail33");
		if (rs.next()) {
			return false;
		} else {
			return true;
		}
	}

	public User login(String name) throws Exception {
		String SQL = "select * from VS_USER where name=?";
		Connection conn = DbUtil.getConnection();
		PreparedStatement pst = conn.prepareStatement(SQL);
		pst.setString(1, name);
		ResultSet rs = pst.executeQuery();
		User pro = new User();
		while (rs.next()) {
			pro.setEmail(rs.getString("email"));
			pro.setPassword(rs.getString("password"));
			pro.setName(rs.getString("name"));
			pro.setTel(rs.getString("tel"));
		}
		return pro;

	}

	public void release(ChallengeInfo paly) throws Exception {
		String rel = "insert into VSPLAY (challenger,play,area,money,challengerPalyid) values(?,?,?,?,?)";
		Connection conn = DbUtil.getConnection();
		PreparedStatement pst = conn.prepareStatement(rel);
		pst.setString(1, paly.getChellname());
		pst.setString(2, paly.getPlay());
		pst.setString(3, paly.getArea());
		pst.setDouble(4, paly.getMoney());
		pst.setInt(5, paly.getChellplayid());
		pst.executeUpdate();
		DbUtil.close(conn);
	}
	/*
	 * 挑战事件
	 * @see dao.Userdao#war(int, java.lang.String)
	 */
	public boolean war(int id, User user) throws Exception {
		//获取当前日期
		Date date = new Date(System.currentTimeMillis());
		String sql = " update VSPLAY set accept= ? and acceptid = ? and acctime = ? and status = 1 where id= ? ";
		Connection conn = DbUtil.getConnection();
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setString(1, user.getName());
		pst.setInt(2, user.getId());
		pst.setDate(3, date);
		pst.setInt(4, id);
		pst.executeUpdate();
		DbUtil.close(conn);
		return true;		
	}

	public void image(String imag, int id) throws Exception {
		String ima = " update VSPLAY set image=? where id=? ";
		Connection conn = DbUtil.getConnection();
		PreparedStatement pst = conn.prepareStatement(ima);
		pst.setString(1, imag);
		pst.setInt(2, id);
		pst.executeUpdate();
		DbUtil.close(conn);

	}

}
